#!/bin/bash

# Name             :ora_primary_backup_rman.sh
# Description      :oracle数据库恢复备库
# Modification log :

#定义常量
# typeset ORA_PRI_USER="@@ORA_PRI_USER@@" #sys
# typeset ORA_PRI_PASSWD="@@ORA_PRI_PASSWD@@" #sys
# typeset ORA_SLA_USER="@@ORA_SLA_USER@@" #sys
# typeset ORA_SLA_PASSWD="@@ORA_SLA_PASSWD@@" #sys
# typeset ORA_PRI_IP='@@ORA_PRI_IP@@'               #主服务器ip 10.27.5.199
# typeset ORA_SLA_IP='@@ORA_SLA_IP@@'               #从服务器ip 10.27.5.139
# typeset ORA_PRI_TNS_PORT='@@ORA_PRI_TNS_PORT@@' #1521
# typeset ORA_SLA_TNS_PORT='@@ORA_SLA_TNS_PORT@@' #1521
# typeset ORA_SLA_SSH_PORT='@@ORA_SLA_SSH_PORT@@' #22
# typeset ORA_PRI_HOST_USER='@@ORA_PRI_HOST_USER@@'                   #主服务器远程用户 oracle
# typeset ORA_PRI_HOST_PASSWD='@@ORA_PRI_HOST_PASSWD@@'           #主服务器远程密码 oracle
# typeset ORA_SLA_HOST_USER='@@ORA_SLA_HOST_USER@@'                   #从服务器远程用户 oracle
# typeset ORA_SLA_HOST_PASSWD='@@ORA_SLA_HOST_PASSWD@@'           #从服务器远程密码 oracle
# typeset check_times='@@check_times@@'                  #命令重复次数
# typeset ORA_PRI_SID='@@ORA_PRI_SID@@'                       #设置主库SID orcl
# typeset ORA_PRI_SID_UP=$(echo "${ORA_PRI_SID}" | tr '[a-z]' '[A-Z]') #ORCL
# typeset ORA_PRI_SID_DOWN=$(echo "${ORA_PRI_SID}" | tr '[A-Z]' '[a-z]') #ORCL
# typeset ORA_SLA_SID='@@ORA_SLA_SID@@'                       #设置从库SID orcl
# typeset ORA_PRI_DB_UNIQUE_NAME='@@ORA_PRI_DB_UNIQUE_NAME@@'            #设置主库uniqname orcl_pri
# typeset ORA_SLA_DB_UNIQUE_NAME='@@ORA_SLA_DB_UNIQUE_NAME@@'            #设置从库uniqname orcl_stl
# typeset ORA_PRI_SERVICE_NAME='@@ORA_PRI_SERVICE_NAME@@' #fsda.pri orcl_pri.pri
# typeset ORA_SLA_SERVICE_NAME='@@ORA_SLA_SERVICE_NAME@@' #orcl_stl.stl orcl_stl.pri
# typeset ORA_PRI_IP='10.27.5.199'               #主服务器ip
# typeset ORA_SLA_IP='10.27.5.139'               #从服务器ip
# typeset ORA_PRI_TNS_PORT='1521'
# typeset ORA_SLA_TNS_PORT='1521'
# typeset ORA_SLA_SSH_PORT='22' #22
# typeset ORA_PRI_HOST_USER='oracle'                   #主服务器远程用户
# typeset ORA_PRI_HOST_PASSWD='oracle'           #主服务器远程密码
# typeset ORA_SLA_HOST_USER='oracle'                   #从服务器远程用户
# typeset ORA_SLA_HOST_PASSWD='oracle'           #从服务器远程密码
# typeset check_times='5'                  #命令重复次数
# typeset ORA_PRI_SID='orcl'                       #设置主库SID orcl
# typeset ORA_PRI_SID_UP=$(echo "${ORA_PRI_SID}" | tr '[a-z]' '[A-Z]') #ORCL
# typeset ORA_PRI_SID_DOWN=$(echo "${ORA_PRI_SID}" | tr '[A-Z]' '[a-z]') #ORCL
# typeset ORA_SLA_SID='orcl'                       #设置从库SID orcl
#typeset ORA_SLA_DB_UNIQUE_NAME='orcl_stl'            #设置从库uniqname orcl_stl
# typeset ORA_PRI_SERVICE_NAME='orcl_pri.pri' #fsda.pri orcl_pri.pri
# typeset ORA_SLA_SERVICE_NAME='orcl_stl.pri' #orcl_stl.stl orcl_stl.pri
# typeset ORA_PRI_DB_UNIQUE_NAME='orcl_pri'            #设置主库uniqname orcl_pri
# typeset ORA_SLA_DB_UNIQUE_NAME='orcl_stl02'    #slave2的uniquename
# typeset ORA_SLA_DB_UNIQUE_NAME='orcl_stl'            #slave1的uniquename
# typeset ORA_PRI_USER="sys" #sys
# typeset ORA_PRI_PASSWD="sys" #sys
# typeset ORA_SLA_USER="sys" #sys
# typeset ORA_SLA_PASSWD="sys" #sys

### test begin ###
typeset ORA_PRI_DB_UNIQUE_NAME='@@ORA_PRI_DB_UNIQUE_NAME@@'            #设置主库uniqname orcl_pri
typeset ORA_SLA_DB_UNIQUE_NAME='@@ORA_SLA_DB_UNIQUE_NAME@@'    #slave的uniquename
typeset ORA_PRI_USER="@@ORA_PRI_USER@@" #sys
typeset ORA_PRI_PASSWD="@@ORA_PRI_PASSWD@@" #sys
typeset ORA_SLA_USER="@@ORA_SLA_USER@@" #sys
typeset ORA_SLA_PASSWD="@@ORA_SLA_PASSWD@@" #sys
typeset check_times=@@check_times@@
### test end ###

#定义日志目录
#typeset LOCAL_IP=$(/usr/sbin/ip a |grep ens |grep inet | awk '{print $2}' | awk -F'/' '{print $1}')
typeset LOCAL_IP=$(/usr/sbin/ip a|grep `/usr/sbin/ip r|grep default|awk '{print $5}'`|grep inet|awk '{print $2}'|awk -F'/' '{print $1}') # 可能存在多个IP，只取第一个；需实际验证修改
typeset SCRIPT_NAME=$(basename ${0})  
typeset MODULE_NAME=$(basename ${0} | sed 's/\.sh$//')
typeset LOG_PATH='/tmp/dr_log'
[ ! -d ${LOG_PATH} ] && mkdir -p ${LOG_PATH}
typeset LOG_FILE="${LOG_PATH}/${MODULE_NAME}_${LOCAL_IP}.log"
[ ! -f ${LOG_FILE} ] && touch ${LOG_FILE}

function log_format()
{
    local log_level=$1
    local log_info=$2
    echo "[`date +"%F %H:%M:%S"`] [${log_level}] ${log_info}" | tee -a ${LOG_FILE}
}

#设置oracle环境变量
source ~/.bash_profile

#检查check次数
[ ! ${check_times} ] && typeset check_times='60'
expr 1 + ${check_times} &>/dev/null
if [ $? -ne 0 ];then
    log_format "ERROR" 'Argument needs to be integer' && log_format "ERROR" "script_execute_error!" && exit 1
fi

# cheres=false
# for ((i=1;i<=$(($check_times));i++))
# do
#   sleep 3
# a=$(sqlplus ${ORA_SLA_USER}/${ORA_SLA_PASSWD}@${ORA_SLA_DB_UNIQUE_NAME} as sysdba << eof |grep 'SQL>'|wc -l
# exit
# eof
# )

# b=$(sqlplus ${ORA_PRI_USER}/${ORA_PRI_PASSWD}@${ORA_PRI_DB_UNIQUE_NAME} as sysdba << eof |grep 'SQL>'|wc -l
# exit
# eof
# )
#   if [ $a -eq 1 ] && [ $b -eq 1];then
#     cheres=true
#     break
#   else
#     if [ $a -ne 1 ];then
#       log_format "WARNING" "try to connect to slave oracle"
#     fi
#     if [ $b -ne 1 ];then
#       log_format "WARNING" "try to connect to master oracle"
#     fi
#   fi
# done

# if [ $cheres == "true" ];then
#   log_format "INFO" "success to connect to slave"
# else
#   log_format "ERROR" "can not connet to slave !"
#   log_format "ERROR" "script_execute_error!" && exit 1
# fi

cheres=false
for ((i=1;i<=$(($check_times));i++))
do
rman target ${ORA_PRI_USER}/${ORA_PRI_PASSWD}@${ORA_PRI_DB_UNIQUE_NAME} auxiliary ${ORA_SLA_USER}/${ORA_SLA_PASSWD}@${ORA_SLA_DB_UNIQUE_NAME} << EOFAA
exit
EOFAA
if [ $? -ne 0 ];then
  sleep 3
  log_format "ERROR" "rman connected to target database failed.try agin ..."
else
  cheres=true
  break
fi
done

if [ $cheres == "true" ];then
  log_format "INFO" "success to connect to slave"
else
  log_format "ERROR" "can not connet to slave !"
  log_format "ERROR" "script_execute_error!" && exit 1
fi

rman target ${ORA_PRI_USER}/${ORA_PRI_PASSWD}@${ORA_PRI_DB_UNIQUE_NAME} auxiliary ${ORA_SLA_USER}/${ORA_SLA_PASSWD}@${ORA_SLA_DB_UNIQUE_NAME} << EOFBB
duplicate target database for standby from active database dorecover nofilenamecheck;
EOFBB
if [ $? -ne 0 ]
then
  log_format "ERROR" "duplicate target database for standby failed."
  log_format "ERROR" "script_execute_error!" && exit 10
fi

log_format "INFO" "duplicate target database for standby successfully."

MAIN_RET=$?
log_format "INFO" "MAIN_RET=${MAIN_RET}"

if [[ "$MAIN_RET" == 0 ]]
then
    log_format "INFO" "script_execute_success!"
else
    log_format "ERROR" "script_execute_error!"
fi

exit $MAIN_RET
